package com.ruoyi.tuihuo.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.domain.Kucunpandian;
import com.ruoyi.cangku.mapper.CangkuMapper;
import com.ruoyi.cangku.mapper.KucunpandianMapper;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.goods.domain.Shangpinxinxi;
import com.ruoyi.goods.mapper.ShangpinxinxiMapper;
import com.ruoyi.tuihuo.enums.TuihuoType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tuihuo.mapper.TuihuoMapper;
import com.ruoyi.tuihuo.domain.Tuihuo;
import com.ruoyi.tuihuo.service.ITuihuoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 购货退货Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-23
 */
@Service
public class TuihuoServiceImpl implements ITuihuoService
{
    @Autowired
    private TuihuoMapper tuihuoMapper;

    @Autowired
    private KucunpandianMapper kucunpandianMapper;

    @Autowired
    private CangkuMapper cangkuMapper;

    @Autowired
    private ShangpinxinxiMapper shangpinxinxiMapper;

    /**
     * 查询购货退货
     *
     * @param id 购货退货主键
     * @return 购货退货
     */
    @Override
    public Tuihuo selectTuihuoById(Long id)
    {
        return tuihuoMapper.selectTuihuoById(id);
    }

    /**
     * 查询购货退货列表
     *
     * @param tuihuo 购货退货
     * @return 购货退货
     */
    @Override
    public List<Tuihuo> selectTuihuoList(Tuihuo tuihuo)
    {
        return tuihuoMapper.selectTuihuoList(tuihuo);
    }

    /**
     * 新增购货退货
     *
     * @param tuihuo 购货退货
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTuihuo(Tuihuo tuihuo)
    {
        Kucunpandian filter = new Kucunpandian();
        filter.setGoodId(tuihuo.getGoodId());
        filter.setCangkuId(tuihuo.getTuihuoCangkuId());

        Kucunpandian kucun = kucunpandianMapper.selectKucunpandianByGoodIdAndCangkuId(filter);
        Shangpinxinxi shangpin =  shangpinxinxiMapper.selectShangpinxinxiById(tuihuo.getGoodId());
        Cangku cangku = cangkuMapper.selectCangkuById(tuihuo.getTuihuoCangkuId());

        if(kucun == null) throw new RuntimeException("该商品没有入库记录");
        BigDecimal oldCount = kucun.getKuncun();
        if(tuihuo.getTuihuoType() == TuihuoType.SALES.ordinal()){
           BigDecimal nowCount = oldCount.add(tuihuo.getShuliang());
            kucun.setKuncun(nowCount);
            kucunpandianMapper.updateKucunpandian(kucun);

            tuihuo.setDanjia( shangpin.getShangpinLingshoujia() );
            tuihuo.setJiner( tuihuo.getShuliang().multiply(shangpin.getShangpinLingshoujia()));

        }
        if(tuihuo.getTuihuoType() == TuihuoType.PURCHASE.ordinal()){
           if(oldCount.compareTo(tuihuo.getShuliang()) > -1){
               BigDecimal nowCount = oldCount.subtract(tuihuo.getShuliang());
               kucun.setKuncun(nowCount);
               kucunpandianMapper.updateKucunpandian(kucun);

               tuihuo.setDanjia( shangpin.getShangpinPifajia() );
               tuihuo.setJiner( tuihuo.getShuliang().multiply(shangpin.getShangpinPifajia()));
           }else{
               throw new RuntimeException("仓库商品库存不足,仓库数量:"+ oldCount +",退货数量:"+tuihuo.getShuliang());
           }
        }
        tuihuo.setTuihuoDanhao(OrderUtils.createOrderSN());
        tuihuo.setTuihuoCangku(cangku.getCangkuName());
        tuihuo.setGoodName(shangpin.getShangpinName());
        tuihuo.setGoodGuige(shangpin.getShangpinGuige());
        tuihuo.setGoodBianma(shangpin.getShangpinId());

        return tuihuoMapper.insertTuihuo(tuihuo);
    }

    /**
     * 修改购货退货
     *
     * @param tuihuo 购货退货
     * @return 结果
     */
    @Override
    public int updateTuihuo(Tuihuo tuihuo)
    {
        return tuihuoMapper.updateTuihuo(tuihuo);
    }

    /**
     * 批量删除购货退货
     *
     * @param ids 需要删除的购货退货主键
     * @return 结果
     */
    @Override
    public int deleteTuihuoByIds(Long[] ids)
    {
        return tuihuoMapper.deleteTuihuoByIds(ids);
    }

    /**
     * 删除购货退货信息
     *
     * @param id 购货退货主键
     * @return 结果
     */
    @Override
    public int deleteTuihuoById(Long id)
    {
        return tuihuoMapper.deleteTuihuoById(id);
    }
}
